  //
//  CellRCConversationViewController.m
//  HandheldHospital
//
//  Created by 张鹏的电脑 on 16/3/5.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "CellRCConversationViewController.h"
#import "DataManager.h"

#define kShared [DataManager defaultManager]

@implementation CellRCConversationViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if([kShared.userType isEqualToString:@"普通用户"])
    {
        NSString *string = nil;
        if(![kShared.collectDoctorArray containsObject:self.seek])
        {
            string = @"收藏";
        }
        else
        {
            string = @"取消收藏";
        }
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:string style:(UIBarButtonItemStylePlain) target:self action:@selector(didClickCollectSender:)];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationItem.title = self.titleName;
    self.conversationMessageCollectionView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"hh"]];

}
- (void)didClickCollectSender:(UIBarButtonItem *)sender
{
    //判断点击的是收藏还是取消收藏
    if([sender.title isEqualToString:@"收藏"])
    {
        //如果点击的是收藏，就将当前医生收藏到数据库中
        [kShared.collectDoctorArray addObject:self.seek];
        [[AVUser currentUser] setObject:kShared.collectDoctorArray forKey:@"collectDoctor"];
        [[AVUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(succeeded)
            {
                sender.title = @"取消收藏";
                AVObject *manager = [AVObject objectWithClassName:@"ManagerArray"];
                [manager setObject:self.seek.currentUser forKey:@"doctor"];
                [manager setObject:[AVUser currentUser].username forKey:@"managerName"];
                [manager setObject:[AVUser currentUser] forKey:@"manager"];
                [manager saveInBackground];
            }
        }];
        
    }
    else
    {
        //如果点击的是取消收藏，就将当前医院信息从数据库中删除
        [kShared.collectDoctorArray removeObject:self.seek];
        [[AVUser currentUser] setObject:kShared.collectDoctorArray forKey:@"collectDoctor"];
        [[AVUser currentUser] saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(succeeded)
            {
                sender.title = @"收藏";
                AVQuery *query = [AVQuery queryWithClassName:@"ManagerArray"];
                [query whereKey:@"doctor" equalTo:self.seek.currentUser];
                [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    NSArray<AVObject *> *todos = objects;
                    for (AVObject *todo in todos) {
                        if([todo[@"managerName"] isEqualToString:[AVUser currentUser].username])
                        {
                            [todo delete];
                        }
                    }
                    [AVObject saveAll:todos];
                }];
            }
        }];
    }
}

/*
 即将显示消息Cell的回调
 
 @param cell        消息Cell
 @param indexPath   该Cell对应的消息Cell数据模型在数据源中的索引值
 
 @discussion 您可以在此回调中修改Cell的显示和某些属性。
 */
- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell
                   atIndexPath:(NSIndexPath *)indexPath{
    
    if ([cell isMemberOfClass:[RCTextMessageCell class]]) {
        RCTextMessageCell *textM = (RCTextMessageCell *)cell;
        UILabel * textL = (UILabel *) textM.textLabel;
        textL.textColor = [UIColor orangeColor];
    }
    
}


@end
